package net.villonanny.type;

import java.util.HashMap;

/**
 * 
 * @deprecated
 */
public enum TroopTypeByTribe {
	// String values must match configuration file tags
	// ROMAN
	LEGIONNAIRE(TribeType.ROMANS, 1, "legionnaire", BuildingType.BARRACKS),
	PRAETORIAN(TribeType.ROMANS, 2, "praetorian", BuildingType.BARRACKS),
	IMPERIAN(TribeType.ROMANS, 3, "imperian", BuildingType.BARRACKS),
	EQUITES_LEGATI(TribeType.ROMANS, 4, "equitesLegati", BuildingType.STABLE),
	EQUITES_IMPERATORIS(TribeType.ROMANS, 5, "equitesImperatoris", BuildingType.STABLE),
	EQUITES_CAESARIS(TribeType.ROMANS, 6, "equitesCaesaris", BuildingType.STABLE),
	BATTERING_RAM(TribeType.ROMANS, 7, "batteringRam", BuildingType.WORKSHOP),
	FIRE_CATAPULT(TribeType.ROMANS, 8, "fireCatapult", BuildingType.WORKSHOP),
	SENATOR(TribeType.ROMANS, 9, "senator", BuildingType.RESIDENCE),
	ROMAN_SETTLER(TribeType.ROMANS, 10, "settler", BuildingType.RESIDENCE),
	// TEUTON
	MACEMAN(TribeType.TEUTONS, 1, "maceman", BuildingType.BARRACKS),
	SPEARMAN(TribeType.TEUTONS, 2, "spearman", BuildingType.BARRACKS),
	AXEMAN(TribeType.TEUTONS, 3, "axeman", BuildingType.BARRACKS),
	SCOUT(TribeType.TEUTONS, 4, "scout", BuildingType.STABLE),
	PALADIN(TribeType.TEUTONS, 5, "paladin", BuildingType.STABLE),
	TEUTONIC_KNIGHT(TribeType.TEUTONS, 6, "teutonicKnight", BuildingType.STABLE),
	TEUTONIC_RAM(TribeType.TEUTONS, 7, "ram", BuildingType.WORKSHOP),
	CATAPULT(TribeType.TEUTONS, 8, "catapult", BuildingType.WORKSHOP),
	CHIEF(TribeType.TEUTONS, 9, "chief", BuildingType.RESIDENCE),
	TEUTONIC_SETTLER(TribeType.TEUTONS, 10, "settler", BuildingType.RESIDENCE),
	// GAUL
	PHALANX(TribeType.GAULS, 1, "phalanx", BuildingType.BARRACKS),
	SWORDSMAN(TribeType.GAULS, 2, "swordsman", BuildingType.BARRACKS),
	PATHFINDER(TribeType.GAULS, 3, "pathfinder", BuildingType.STABLE),
	THEUTATES_THUNDER(TribeType.GAULS, 4, "theutatesThunder", BuildingType.STABLE),
	DRUIDRIDER(TribeType.GAULS, 5, "druidrider", BuildingType.STABLE),
	HAEDUAN(TribeType.GAULS, 6, "haeduan", BuildingType.STABLE),
	GALLIC_RAM(TribeType.GAULS, 7, "ram", BuildingType.WORKSHOP),
	TREBUCHET(TribeType.GAULS, 8, "trebuchet", BuildingType.WORKSHOP),
	CHIEFTAIN(TribeType.GAULS, 9, "chieftain", BuildingType.RESIDENCE),
	GALLIC_SETTLER(TribeType.GAULS, 10, "settler", BuildingType.RESIDENCE)
	// TODO: Complete the list:
	// NATURE
	;
	// This is needed to convert from int to enum
	private static HashMap<Integer, TroopTypeByTribe> fromIntMap;
	static {
		fromIntMap = new HashMap<Integer, TroopTypeByTribe>();
		TroopTypeByTribe[] values = TroopTypeByTribe.values();
		for (int i = 0; i < values.length; i++) {
			TroopTypeByTribe m = values[i];
			fromIntMap.put(m.ordinal(), m);
		}
	}
	private String stringValue;
	private int troopId;
	private TribeType tribe;
	private BuildingType building;

	private TroopTypeByTribe(TribeType tribe, int troopId, String name, BuildingType building) {
		this.tribe = tribe;
		this.troopId = troopId;
		this.stringValue = name;
		this.building = building;
	}
	
	public String toString() {
		return stringValue;
	}

	public static TroopTypeByTribe fromTribeId(TribeType tribe, int troopId) {
		for (TroopTypeByTribe type: TroopTypeByTribe.values()) {
			if (type.tribe() == tribe && type.troopId == troopId) {
				return type;
			}
		}
		return null;
	}
	
	public static TroopTypeByTribe fromTribeString(TribeType tribe, String troop) {
		if (troop == null) {
			return null;
		}
		for (TroopTypeByTribe type: TroopTypeByTribe.values()) {
			if (type.tribe() == tribe && type.toString().equals(troop.toLowerCase())) {
				return type;
			}
		}
		return null;
	}

	/**
	 * Detecting a troop by its image
	 * @param num image number
	 * @return
	 */
	public static TroopTypeByTribe fromInt(int num) {
		return fromIntMap.get(num-getFirstIndex());
	}

	/**
	 * Get the troop id
	 * @return
	 */
	public int toTroopId() {
		return troopId;
	}
	
	/**
	 * Get the first value of a troop id
	 * @return
	 */
	public static int firstTroopId() {
		return 1; // XXX: Should this be calculated?
	}
	
	/**
	 * Get the max value of a troop id
	 * @return
	 */
	public static int lastTroopId() {
		return 10; // XXX: Should this be calculated?
	}

	/**
	 * Get the troop image number
	 * @return
	 */
	public int toIndex() {
		return ordinal() + getFirstIndex();
	}
	
	/**
	 * Get the troop tribe
	 * @return
	 */
	public TribeType tribe() {
		return tribe;
	}
	
	public BuildingType building() {
		return building;
	}
	
	public static int size() {
		return TroopTypeByTribe.values().length;
	}
	
	public static int getFirstIndex() {
		return 1;  // Travian is 1-based
	}

	/**
	 * Last index, inclusive
	 * @return
	 */
	public static int getLastIndex() {
		return size() + getFirstIndex() - 1;
	}
}